import store from '@/store'
import { setTagNavListInLocalstorage } from '@/libs/util'

// 生成单店菜单，并保存信息
export function createMenuListWithStore(userId, storeInfo, storeMenuList) {
  let menuList = commonDisposeMenuList(storeMenuList);
  for (let i = 0; i < menuList.length; i++) {
    const element = menuList[i];
    if (element.title == '数据报表中心') {
      let returnHistoryDic = { children: [], icon: "ios-disc", id: 22, meta: { icon: "", title: "退货记录表" }, name: "returnHistory", path: "returnHistory", sortNo: 3, title: "退货记录表" };
      element.children.splice(3, 0, returnHistoryDic);
      continue;
    }
    // if (element.title == '商品管理') {
    //   dataManageCenterIndex = i;
    // }
  }
  //   let returnHistoryDic = {children: [], icon: "ios-disc", id: 22, meta: {icon: "", title: "退货记录表"}, name: "returnHistory", path: "returnHistory", sortNo: 3, title: "退货记录表"};
  //   element.children.splice(3,0,returnHistoryDic);

  //   // let sellPromotionDic = {id:50,parentId:null,title:"促销活动",path:"/promotion",name:"promotion",icon:"md-pricetags",meta:{icon:"md-pricetags",title:"促销活动"},children:[{id:51,parentId:5,title:"创建促销活动",path:"createSellPromotion",name:"createSellPromotion",icon:"ios-disc",meta:{icon:"",title:"创建促销活动"},children:[],sortNo:0,menuType:0},{id:52,parentId:5,title:"管理促销活动",path:"manageSellPromotion",name:"manageSellPromotion",icon:"ios-disc",meta:{icon:"",title:"管理促销活动"},children:[],sortNo:0,menuType:0}]};
  //   // menuList.splice(i,0,sellPromotionDic);
  //   break;
  // }
  // }

  saveLoginInfo('store', userId, storeInfo, menuList);
}

// 生成连锁菜单，并保存信息
export function createMenuListWithCompany(userId, storeInfo) {
  let companyMenuList = [{
    id: 50,
    parentId: null,
    title: "促销活动",
    path: "/promotion",
    name: "promotion",
    icon: "md-pricetags",
    meta: {
      icon: "md-pricetags",
      title: "促销活动"
    },
    children:
      [{
        id: 51,
        parentId: 5,
        title: "创建促销活动",
        path: "createSellPromotion",
        name: "createSellPromotion",
        icon: "ios-disc",
        meta: {
          icon: "",
          title: "创建促销活动"
        },
        children: [],
        sortNo: 0,
        menuType: 0
      },
      {
        id: 52,
        parentId: 5,
        title: "管理促销活动",
        path: "manageSellPromotion",
        name: "manageSellPromotion",
        icon: "ios-disc",
        meta: {
          icon: "",
          title: "管理促销活动"
        },
        children: [],
        sortNo: 0,
        menuType: 0
      }]
  },
  {
    "id": 19,
    "title": "数据报表中心",
    "path": "/chainOrder",
    "name": "/chainOrder",
    "icon": null,
    "meta": {
      "icon": "md-analytics",
      "title": "数据报表中心"
    },
    "children": [{
      "id": 20,
      "title": "营运统计表",
      "path": "chainOperateStatistics",
      "name": "chainOperateStatistics",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "营运统计表"
      },
      "children": [],
      "sortNo": 1
    }, {
      "id": 21,
      "title": "实收统计表",
      "path": "chainPayModeStatistics",
      "name": "chainPayModeStatistics",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "实收统计表"
      },
      "children": [],
      "sortNo": 2
    }, {
      "id": 22,
      "title": "订单汇总表",
      "path": "chainOrderTotal",
      "name": "chainOrderTotal",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "订单汇总表"
      },
      "children": [],
      "sortNo": 3
    }, {
      "id": 25,
      "title": "订单明细表",
      "path": "chainOrderFoodDetail",
      "name": "chainOrderFoodDetail",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "订单明细表"
      },
      "children": [],
      "sortNo": 6
    }, {
      "id": 26,
      "title": "会员余额消费",
      "path": "chainMembershipBalanceConsumption",
      "name": "chainMembershipBalanceConsumption",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "会员余额消费"
      },
      "children": [],
      "sortNo": 6
    },
    {
      "id": 27,
      "title": "利润表",
      "path": "profitstatement",
      "name": "profitstatement",
      "icon": null,
      "meta": {
        "icon": "",
        "title": "利润表"
      },
      "children": [],
      "sortNo": 7
    }
    ],
    "sortNo": 9
  },
  {
    "id": 16,
    "title": "分店管理",
    "path": "/chainBranchStoreManagement",
    "name": "/chainBranchStoreManagement",
    "icon": null,
    "meta": {
      "icon": "md-settings",
      "title": "分店管理"
    },
    "sortNo": 1
  }];

  let menuList = commonDisposeMenuList(companyMenuList);
  saveLoginInfo('company', userId, storeInfo, menuList);
}

// 清除localStorage所有缓存
export function clearLocalStorage() {
  localStorage.clear();
  setTagNavListInLocalstorage([]);
  store.commit('setStoreList', []);
  store.commit('setStoreId', '');
  store.commit('setStoreCode', '');
  store.commit('setStoreName', '');
  store.commit('setUserId', '');
}

// 处理菜单列表
function commonDisposeMenuList(menuList) {
  let navlist = menuList;
  let homeIndex = -1;
  for (let i = 0; i < navlist.length; i++) {
    let listName = navlist[i].name; // 获取name值
    let str = listName.replace("/", ""); // 替换
    navlist[i].name = str; // 重新赋值
    // 获取图标
    let iconImg = navlist[i].icon;
    // 赋值空字符串
    if (iconImg == null) {
      navlist[i].icon = navlist[i].meta.icon;
    }
    // 获取父级下面的子级数量以及图标
    let numBer = 0;
    if (
      navlist[i] &&
      navlist[i].children &&
      navlist[i].children.length > 0
    ) {
      numBer = navlist[i].children.length;
    }
    for (let j = 0; j < numBer; j++) {
      let numBerIcon = navlist[i].children[j].icon;
      // 子图图标
      if (numBerIcon == null) {
        navlist[i].children[j].icon = "ios-disc";
      }
    }
    // 如果等于1则显示父级
    if (numBer == 1) {
      navlist[i].meta.showAlways = true;
    }
    if (navlist[i].path == "/singlePage") {
      navlist[i].path = "/";
      navlist[i].name = "_singlePage";
      navlist[i].meta.showAlways = false;
      navlist[i].meta.hideInMenu = true;
      navlist[i].meta.notCache = true;
      navlist[i].hideInMenu = true;
      homeIndex = i;
    } else if (navlist[i].path == "/vip") {
      navlist[i].path = "/";
      navlist[i].name = "vip";
      // navlist[i].meta.showAlways = false;
      // navlist[i].meta.hideInMenu = true;
      navlist[i].meta.href = "https://crmtest.tongfuretail.com/crm/login"; //测试请求网址
      // navlist[i].meta.href = "https://retailcrm.tongfupay.com/crm/menu";
      navlist[i].meta.notCache = true;
      navlist[i].hideInMenu = true;
      console.log(navlist[i],'aaaaaaaa');
    }
  }
  if (homeIndex != -1) {
    navlist.splice(homeIndex, 1);
  }
  return navlist;
}

function saveLoginInfo(userLoginType, userId, storeInfo, menuList) {
  console.log("aaaa");
  localStorage.setItem('userLoginType', userLoginType);
  localStorage.setItem('userId', userId);
  localStorage.setItem('storeInfo', JSON.stringify(storeInfo));
  localStorage.setItem('meunList', JSON.stringify(menuList));
}
